package com.hp.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.hp.ssm.bean.Woom;
import com.hp.ssm.bean.WoomExample;
import com.hp.ssm.service.WoomService;
import com.hp.ssm.util.JsonData;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/woom")
public class WoomColler {

    @Resource
    private WoomService woomService;

    @RequestMapping("/find")
    @ResponseBody
    public Object find(String wtype, @RequestParam(value = "page",defaultValue = "1") Integer page,
                                     @RequestParam(value = "limit",defaultValue = "5") Integer limit) {

        WoomExample example=new WoomExample();
        WoomExample.Criteria criteria=example.createCriteria();
        if (!StringUtils.isNullOrEmpty(wtype)){
            criteria.andWtypeLike("%"+wtype+"%");
        }

        PageInfo<Woom> list=woomService.pageFind(example,page,limit);
        return JsonData.buildSuccess(list);

    }

    @RequestMapping("/findById")
    @ResponseBody
    public Object findById(Integer wid) {
        Woom woom = woomService.findByid(wid);
        return JsonData.buildSuccess(woom);
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object add(Woom woom) {
        int i = woomService.add(woom);
        return i==1? JsonData.buildSuccess("添加成功!"): JsonData.buildError("添加失败！");
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(Woom woom) {
        int i = woomService.update(woom);
        return i==1? JsonData.buildSuccess("修改成功!"): JsonData.buildError("修改失败！");
    }

    @RequestMapping("/del")
    @ResponseBody
    public Object del(Integer wid) {
        int i = woomService.del(wid);
        return i==1? JsonData.buildSuccess("删除成功!"): JsonData.buildError("删除失败！");
    }

    }


